package com.cgc999.host.net;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * 订阅关系管理类
 */
public class SubscriptionManager implements SubscriptionHelper<Object> {
    private CompositeDisposable mDisposable; //存放 Disposable容器
    private static SubscriptionManager subscriptionManager;

    private SubscriptionManager() {
        if (mDisposable == null) {
            mDisposable = new CompositeDisposable();
        }
    }

    public static SubscriptionManager getInstance() {
        if (subscriptionManager == null) {
            synchronized (SubscriptionManager.class) {
                if (subscriptionManager == null) {
                    subscriptionManager = new SubscriptionManager();
                }
            }
        }
        return subscriptionManager;
    }

    /**
     * 添加订阅事件到disposable容器中
     *
     * @param disposable
     */
    @Override
    public void add(Disposable disposable) {
        if (disposable == null) return;
        mDisposable.add(disposable);
    }

    /**
     * 取消订阅事件
     *
     * @param disposable
     */
    @Override
    public void cancel(Disposable disposable) {
        if (disposable != null) {
            mDisposable.delete(disposable);
        }
    }

    @Override
    public void cancelAll() {
        if (mDisposable != null) {
            mDisposable.clear();
        }
    }
}
